import {
  Near
} from '../near/near-model.js';

var near = new Near();

var app = getApp()
Page({
  data: {
    pageIndex: 1,
    isLoadedAll: false,
    loadingHidden: false,
    loadDis:true,
    currentTab: 0,
    currentCity: '',
    disArr:[]
  },
  onLoad: function() {
    this._loadData();
  },
  _loadData: function() {
    var that = this;
    this._getLocation();

  },

  //获取经纬度
  _getLocation: function() {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        // success  
        var longitude = res.longitude
        var latitude = res.latitude
        that.setData({
          longitude: longitude,
          latitude: latitude
        });
        that.loadCity(longitude, latitude)
      },
      fail:function(){
        that.data.loadDis=false;
        that._getStores();
      }
    })
  },
  //获取城市
  loadCity: function(longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=vOESXWdjubU6CXr8D5ULkoivt11qGlyo&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        // success  
        var city = res.data.result.addressComponent.city;
        page.setData({
          currentCity: city,
        });
        page._getStores();
      },
      fail: function() {
        page.setData({
          currentCity: "获取定位失败"
        });
      },
    })
  },
  _getStores: function(callback) {
    var that = this;
      near.getStores(this.data.pageIndex, (res) => {
        var data = res.data;
        that.setData({
          loadingHidden: true
        });
        if (data.data.length <= 0) {
          that.data.isLoadedAll = true; //已经全部加载完毕
          that.data.pageIndex = 1;
        } else {
          that.setData({
            StoreArr: res.data.data,
          });
          //距离保留数组
          if (that.data.loadDis){
            for (var i = 0; i < data.data.length; i++) {
              near.getDistance(that.data.StoreArr[i].loction_x, that.data.StoreArr[i].loction_y, that.data.latitude, that.data.longitude, (res) => {
                that.data.disArr.push(res);
                that.setData({
                  disArr: that.data.disArr,
                });
              });
            }
          }
        }
        callback && callback();
      });
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /*跳转到商品详情*/
  onProductsStoreTap: function(event) {
    var id = near.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../store/store?id=' + id
    })
  },
})